# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.invitedRecordsDao import InvitedRecordsDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertInvitedRecords', methods=['POST'])
def insert_invited_recordsNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'userId': request.json.get('userId', 0),
        'invitedUserId': request.json.get('invitedUserId', 0),
        'code': request.json.get('code', ''),
        'isReward': request.json.get('isReward', 0)
    }

    info = InvitedRecordsDao().insertRecords(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findInvitedRecordsById', methods=['POST'])
def find_invited_records_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    reId = request.json.get('reId', '')
    info = InvitedRecordsDao().findRecordsById(reId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findInvitedRecordsByCondition', methods=['POST'])
def find_invited_records_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    userId = request.json.get('userId', '')
    info = InvitedRecordsDao().findRecordsByCondition(userId=userId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteInvitedRecordsByIds', methods=['POST'])
def delete_invited_records_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = InvitedRecordsDao().deleteRecords(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateInvitedRecordsById', methods=['POST'])
def update_invited_records_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = InvitedRecordsDao().selectInvite(id)

    info = {
        'id': request.json['id'],
        'userId': request.json.get('userId', oldInfo[1]),
        'invitedUserId': request.json.get('invitedUserId', oldInfo[2]),
        'code': request.json.get('code', oldInfo[3]),
        'isReward': request.json.get('isReward', oldInfo[5])
    }

    info = InvitedRecordsDao().updateRecords(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
